package com.xw.tools.view;

import com.xw.tools.model.TextItemVo;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SingleLineWin extends AbstractWin {

    public void start() {
        Stage primaryStage = new Stage();
        primaryStage.setTitle("SingleLineWin");
        Scene scene = new Scene(new VBox(), 800, 495);
        initView(scene, primaryStage);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void initView(Scene scene, Stage primaryStage) {
        TextArea inputTextArea = new TextArea();
        inputTextArea.setWrapText(true);
        TextArea outputTextArea = new TextArea();
        outputTextArea.setWrapText(true);
        outputTextArea.setFont(Font.font(16));
        Label label = new Label("转义符:");
        TextItemVo itemVo1 = new TextItemVo("单引号(,)", "1");
        TextItemVo itemVo2 = new TextItemVo("双引号(\")", "2");
        ChoiceBox choiceBox = new ChoiceBox(FXCollections.observableArrayList(
                itemVo1, itemVo2)
        );
        choiceBox.setValue(itemVo1);
        Button button = new Button("转换");
        button.setOnAction((ActionEvent t) -> {
            String inputText = inputTextArea.getText();
            if (inputText == null || inputText.isEmpty()) {
                return;
            }
            TextItemVo itemVo = (TextItemVo) choiceBox.getValue();
            if (itemVo == null) {
                inputText = inputText.replaceAll("\\r", "");
                inputText = inputText.replaceAll("\\n", "");
            } else {
                if ("1".equals(itemVo.getValue())) {
                    inputText = inputText.replaceAll("\\r", "\\\\r");
                    inputText = inputText.replaceAll("\\n", "\\\\n");
                    inputText = inputText.replaceAll("'", "\\\\'");
                } else {
                    inputText = inputText.replaceAll("\\r", "\\\\r");
                    inputText = inputText.replaceAll("\\n", "\\\\n");
                    inputText = inputText.replaceAll("\"", "\\\\\"");
                }
            }
            outputTextArea.setText(inputText);
        });

        VBox box = (VBox) scene.getRoot();
        VBox.setVgrow(inputTextArea, Priority.ALWAYS);
        box.getChildren().add(inputTextArea);
        HBox hBox = new HBox();
        HBox.setMargin(label, new Insets(3));
        hBox.getChildren().add(label);
        hBox.getChildren().add(choiceBox);
        HBox.setHgrow(label, Priority.ALWAYS);
        HBox.setHgrow(choiceBox, Priority.ALWAYS);
        box.getChildren().add(hBox);
        box.getChildren().add(button);
        VBox.setVgrow(outputTextArea, Priority.ALWAYS);
        box.getChildren().add(outputTextArea);
    }

    @Override
    public void show() {
        start();
    }
}
